Imports Microsoft.VisualBasic
Imports System.Net

Friend Class SendMailObject_CDO
    Inherits SendMailObject_Base

    Public Sub New(ByVal sEmailServer As String)
        MyBase.New(sEmailServer)
    End Sub

    Public Overrides Function SendMessage( _
        ByVal oMessage As Message) As String

        Dim sResult As String = ""

        Try
            Dim oSMTPMessage As Mail.MailMessage = ComposeSMTPMessage(oMessage)

            Dim oSMTPServer As New Mail.SmtpClient(msEmailServer)
            oSMTPServer.Send(oSMTPMessage)

        Catch ex As Exception
            sResult = ex.Message
        End Try

        Return sResult
    End Function

    Protected Function ComposeSMTPMessage( _
        ByVal oMessage As Message) As Mail.MailMessage

        Dim oSMTPMessage As New Mail.MailMessage

        Dim sName As String = ""
        Dim sEmail As String = ""

        '---Sender
        Dim oSenderAddress As New System.Net.Mail.MailAddress(oMessage.SenderEmail, oMessage.SenderName)
        oSMTPMessage.Sender = oSenderAddress
        oSMTPMessage.From = oSenderAddress

        '---Recipients
        Dim sReceipients() As String = Split(oMessage.RecipientAddressList, ";")

        For Each sRecipient As String In sReceipients
            If Trim(sRecipient) > "" Then
                GetAddressParts(sRecipient, sName, sEmail)

                Dim oRecipientAddress As New System.Net.Mail.MailAddress(sEmail, sName)
                oSMTPMessage.To.Add(oRecipientAddress)
            End If
        Next

        '---CC
        If oMessage.RecipientCCList > "" Then
            Dim CC() As String = Split(oMessage.RecipientCCList, ";")
            For Each sRecipient As String In CC
                GetAddressParts(sRecipient, sName, sEmail)

                Dim oRecipientAddress As New System.Net.Mail.MailAddress(sEmail, sName)
                oSMTPMessage.CC.Add(oRecipientAddress)
            Next
        End If

        '---BCC
        If oMessage.RecipientBCCList > "" Then
            Dim BCC() As String = Split(oMessage.RecipientBCCList, ";")
            For Each sRecipient As String In sReceipients
                GetAddressParts(sRecipient, sName, sEmail)

                Dim oRecipientAddress As New System.Net.Mail.MailAddress(sEmail, sName)
                oSMTPMessage.Bcc.Add(oRecipientAddress)
            Next
        End If

        '---Priority
        Dim oPriority As New Mail.MailPriority

        If oMessage.Priority = Message.MsgPriority.High Then
            oPriority = Mail.MailPriority.High

        ElseIf oMessage.Priority = Message.MsgPriority.Low Then
            oPriority = Mail.MailPriority.Low

        Else
            oPriority = Mail.MailPriority.Normal
        End If
        oSMTPMessage.Priority = oPriority

        '---Return recieipt
        If oMessage.ReturnReceipt = True Then
            oSMTPMessage.DeliveryNotificationOptions = Mail.DeliveryNotificationOptions.OnSuccess
        Else
            oSMTPMessage.DeliveryNotificationOptions = Mail.DeliveryNotificationOptions.None
        End If

        '---Attachments (files)
        If oMessage.AttachmentList > "" Then
            Dim sAttachments() As String = Split(oMessage.AttachmentList, ";")
            For Each sAttachment As String In sAttachments
                Dim oAttachment As New Mail.Attachment(sAttachment)

                oSMTPMessage.Attachments.Add(oAttachment)
            Next
        End If

        '---Attachment (text)
        If oMessage.AttachmentText > "" Then
            Dim oAttachment As Mail.Attachment = Mail.Attachment.CreateAttachmentFromString( _
                                                                    oMessage.AttachmentText, _
                                                                    "JobInfo", _
                                                                    System.Text.Encoding.ASCII, _
                                                                    System.Net.Mime.MediaTypeNames.Text.Xml)
            oSMTPMessage.Attachments.Add(oAttachment)
        End If

        '---Subject
        If oMessage.Subject > "" Then
            oSMTPMessage.Subject = oMessage.Subject
        End If

        '---Body
        If oMessage.Message > "" Then
            oSMTPMessage.Body = oMessage.Message
        End If

        '---Body can contain HTML
        oSMTPMessage.IsBodyHtml = oMessage.FormatAsHTML

        Return oSMTPMessage
    End Function

    Private Sub GetAddressParts( _
        ByVal sCompleteAddress As String, _
        ByRef sName As String, _
        ByRef sAddress As String)

        Dim iBracketLoc As Integer = sCompleteAddress.IndexOf("<")

        If iBracketLoc = -1 Then
            sName = sCompleteAddress
            sAddress = sCompleteAddress
        Else
            sName = sCompleteAddress.Substring(0, iBracketLoc - 1)
            sAddress = sCompleteAddress.Substring(iBracketLoc + 1, sCompleteAddress.Length - iBracketLoc - 2)
        End If
    End Sub
End Class